package com.geek.javase.ch18.videodemo.reflect;

import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;

/*
 *@Date 2025/11/5
 *@Author wangpeng
 *@Description
 *  *  * 反射中常用方法
 *  * 1.public static Class<?> forName(String className) 获取Class对象信息
 *  * 2.public T newInstance()  创建实例
 *  * 3.public String getName() 返回全类名
 *  * 4.public ClassLoader getClassLoader() 获得类加载器
 *  * 5.public native Class<? super T> getSuperclass(); 获取父类信息
 *  * 6.public Package getPackage()  获取包信息
 *  * 7.public String getSimpleName() 获取类名
 *  * 8.public Class<?>[] getDeclaredClasses() 获取内部类信息
 *  * 9.public InputStream getResourceAsStream(String name) 获得类的同级目录下的资源信息
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //通过全类名获取Class对象
        Class<?> clazz = Class.forName("com.geek.javase.ch18.videodemo.reflect.Student");
        System.out.println("类名："+clazz.getSimpleName());
        System.out.println("全类名："+clazz.getName());
        System.out.println("包："+clazz.getPackage().getName());

        //获取父类信息
        Class<?> superclass = clazz.getSuperclass();
        System.out.println("父类信息："+superclass);

        //获取内部类信息
        Class<?>[] innerclass = clazz.getDeclaredClasses();
        System.out.println("内部类信息："+ Arrays.toString(innerclass));

        //获取与当前类同级目录下的资源信息
        InputStream is = clazz.getResourceAsStream("");
        URL url = clazz.getResource("");

        //创建实例
        Student stu = (Student)clazz.newInstance();
        System.out.println(stu);

        //获取类加载器
        ClassLoader classLoader = clazz.getClassLoader();
        System.out.println("类加载器："+classLoader);

    }
}
